<?php
/**
**百度直达号
**请求接收地址
**Time 2015-11-04 pm 
**User by pzp
**/
include('/var/www/html/new/d/module/api/function.class.php');
include('/var/www/html/new/d/module/api/bd_zhidahao/baidu_zdh.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$BD_zdh = new BD_zdh();
$PFT_infunction = new AllFunction(); 

if(!$_REQUEST) exit('{"error_code":"200000","error_msg":"请传送正确数据"}');
$BD_zdh->logfile(json_encode($_REQUEST),'bdzdh_get_data');

$cmd = $_REQUEST['cmd'];
$msg = $_REQUEST['msg'];
$sign = $_REQUEST['sign'];
$timestamp = $_REQUEST['timestamp'];

$account = '504025';
$password = '5446f60483f3e3163189b84ff45054c6';

$msg = json_decode($msg,true);

if($cmd == 'order_add'){ //下单
	$orderId = $msg['orderId'];						//百度订单号
	$customerName = $msg['customerName'];			//用户姓名
	$customerMobile = $msg['customerMobile'];		//用户手机号
	$citizenIdNumber = $msg['citizenIdNumber'];		//用户身份证号
	$subOrderId = $msg['subOrders'][0]['subOrderId'];		//百度子订单
	$itemNo =  explode('|',$msg['subOrders'][0]['itemNo']);//券商商品id
	$quantity =$msg['subOrders'][0]['quantity'];			//购买数量
	$price = $msg['subOrders'][0]['price'] ;				//商品单价
	$enableTime = $msg['subOrders'][0]['enableTime'];		//商品可使用的起始
	
	$submitO_array = array(
		'account'	=> $account ,
		'password'	=> $password ,
		'lid'		=> $itemNo[1] ,
		'tid'		=> $itemNo[2] ,
		'remoorder'	=> $orderId ,
		'tprice'	=> $price ,
		'num'		=> $quantity ,
		'playtime'	=> date('Y-m-d',$enableTime) , 
		'name'		=> $customerName ,
		'tel'		=> $customerMobile ,
		'sms'		=> 1 ,
		'paystatus'	=> 2 ,
		'tuanhao'	=> '' ,
		'aid'		=> $itemNo[0] ,
		'IDcard'	=> $citizenIdNumber 
	); 	
	
	if(!$itemNo[2]) exit('{"error_code":"200000","error_msg":"产品id格式错误"}');
	
	$select_uuid = "select pid from uu_jq_ticket where id=".$itemNo[2]." limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit('{"error_code":"200000","error_msg":"无此产品id"}');	
	$pid = $GLOBALS['le']->f('pid');  //查询票类id	
	 
	$sellPrice_ldata = $PFT_infunction->Dynamic_Price_And_Storage('504025',$pid,date('Y-m-d',$enableTime),1,$itemNo[0]);
	if($sellPrice_ldata == 105 || $sellPrice_ldata == 1065) exit('{"error_code":"100006"}');
	$sellPrice_ldata_xml =simpleXML_load_string($sellPrice_ldata);
	$sellPrice = (string)$sellPrice_ldata_xml->Rec->UUsprice;
	if($sellPrice != $price) exit('{"error_code":"100003","error_msg":"销售价格不符"}');
	
	$request = $PFT_infunction->outSide_Submit($submitO_array);
	$request_xml = simplexml_load_string($request);
	if((string)$request_xml->Rec->UUordernum){ //下单成功或者1075远端订单重复
		$ecodeDetail[] = (string)$request_xml->Rec->UUcode;
		$order[] = array(
			'subOrderId'	=> $subOrderId ,
			'subOrderNo'	=> (string)$request_xml->Rec->UUordernum ,
			'ecode'			=> (string)$request_xml->Rec->UUcode ,
			'qrUrl'			=> (string)$request_xml->Rec->UUqrcodeIMG ,
			'ecodeDetail'	=> $ecodeDetail
		);
		
		$orderNo = (string)$request_xml->Rec->UUordernum ; //取一个票付通的订单号作为主订单号
		$ecode = (string)$request_xml->Rec->UUcode; 
		// $qrUrl  = (string)$request_xml->Rec->UUqrcodeIMG;
		$qrUrl  = $BD_zdh->get_imgUrl((string)$request_xml->Rec->UUqrcodeIMG);
		$UUtnum  = (int)$request_xml->Rec->UUtnum;
		
		$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$orderNo','$orderId','$subOrderId',13,0,$UUtnum,'$ecode',0,now())";
		$GLOBALS['le']->query($str);
		
		$response_all = array(
			'orderId'	=> $orderId ,
			'orderNo'	=> $orderNo ,
			'ecode'		=> $ecode ,
			'qrUrl'		=> $qrUrl ,
			'subOrders'	=> $order
		);	
		$BD_zdh->logfile($orderId.'|'.$subOrderId.'|'.json_encode($order),'bdzdh_order_s');
		exit(json_encode($response_all));		
	} 
	else{
		$BD_zdh->logfile($orderId.'|'.$subOrderId.'|'.(string)$request_xml->Rec->UUerrorcode,'bdzdh_order_f');
		$error_code = (string)$request_xml->Rec->UUerrorcode;
		if((string)$request_xml->Rec->UUerrorcode == 1062 || (string)$request_xml->Rec->UUerrorcode == 1063)
		$error_code = '100005';
		exit('{"error_code":"'.$error_code.'"}');
	}
}elseif($cmd == 'order_info'){ //查询订单

	$orderId = $msg['orderId'];						//百度订单号
	$subOrderId = $msg['subOrders'][0];

	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId);
	if(!$pft_order_xml) exit('{"error_code":"123"}');
	$qrUrl  = $BD_zdh->get_imgUrl("http://121.199.25.173/open/qr?".(string)$pft_order_xml->Rec->UUsalerid.(string)$pft_order_xml->Rec->UUcertnum);
	$order[] = array(
		'subOrderId'	=> $subOrderId ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'ecode'			=> (string)$pft_order_xml->Rec->UUcode ,
		'qrUrl'			=> $qrUrl ,
		'ecodeDetail'	=> ""
	);
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'ecode'		=> (string)$pft_order_xml->Rec->UUcode ,
		'qrUrl'		=> $qrUrl ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_orderinfo_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'check_list'){	//核销订单列表

	$orderId = $msg['orderId'];						//百度订单号
	$subOrderId = $msg['subOrders'][0];
	if(!$subOrderId){
		$selorder = "select tempOrder from all_api_order where apiOrder='$orderId' limit 1";
		$GLOBALS['le']->query($selorder);
		$GLOBALS['le']->fetch_assoc();
		$subOrderId = $GLOBALS['le']->f('tempOrder');
	}
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId);
	if(!$pft_order_xml) exit('{"error_code":"123"}');	
	$UUstatus = (int)$pft_order_xml->Rec->UUstatus;
	if($UUstatus == 1){
		$checkList[] = array(
			'checkTime'	=> time((string)$pft_order_xml->Rec->UUdtime),
			'checkNum'	=> (int)$pft_order_xml->Rec->UUtnum,
		);
	}
	else $checkList = array();
	
	$order[] = array(
		'subOrderId'	=> $subOrderId ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'checkList'		=> $checkList ,
	);
	
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_ordercheck_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'refund_list'){	//退票订单列表
	
	$orderId = $msg['orderId'];						//百度订单号
	$subOrderId = $msg['subOrders'][0];
	
	if(!$subOrderId){
		$selorder = "select tempOrder from all_api_order where apiOrder='$orderId' limit 1";
		$GLOBALS['le']->query($selorder);
		$GLOBALS['le']->fetch_assoc();
		$subOrderId = $GLOBALS['le']->f('tempOrder');
	}
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId);
	if(!$pft_order_xml) exit('{"error_code":"123"}');	
	$UUstatus = (string)$pft_order_xml->Rec->UUstatus;
	if($UUstatus == 3){
		$refundList[] = array(
			'refundTime'	=> time((string)$pft_order_xml->Rec->UUctime),
			'refundNum'	=> (int)$pft_order_xml->Rec->UUtnum,
			'ecodeList'	=> "" ,
			'status'	=> 2
		);
	}
	else $refundList = '';
	
	$order[] = array(
		'subOrderId'	=> $subOrderId ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'refundList'	=> $refundList ,
	);
	
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_orderrefund_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'unused_list'){	//未使用订单列表
	
	$orderId = $msg['orderId'];						//百度订单号
	$subOrderId = $msg['subOrders'][0];
	if(!$subOrderId){
		$selorder = "select tempOrder from all_api_order where apiOrder='$orderId' limit 1";
		$GLOBALS['le']->query($selorder);
		$GLOBALS['le']->fetch_assoc();
		$subOrderId = $GLOBALS['le']->f('tempOrder');
	}
	
	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId);
	if(!$pft_order_xml) exit('{"error_code":"123"}');
	$UUstatus = (int)$pft_order_xml->Rec->UUstatus;
	$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
	if($UUstatus == 0){
		$unusedList[] = array(
			'unusedNum'	=> $UUtnum,
			'ecodeList'	=> "" ,
		);
	}
	else $unusedList = '';
	
	$order[] = array(
		'subOrderId'	=> $subOrderId ,
		'subOrderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'unusedList'	=> $unusedList ,
	);
	
	$response_all = array(
		'orderId'	=> $orderId ,
		'orderNo'	=> (string)$pft_order_xml->Rec->UUordernum ,
		'subOrders'	=> $order
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_ordercheck_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'qr_image'){	//获取二维码图片内容
	$orderId = $msg['orderId'];						//百度订单号

	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId);
	if(!$pft_order_xml) exit('{"error_code":"123"}');
	$qrUrl  = file_get_contents($BD_zdh->get_imgUrl("http://121.199.25.173/open/qr?".(string)$pft_order_xml->Rec->UUsalerid.(string)$pft_order_xml->Rec->UUcertnum));
	$response_all = array(
		'contentType'	=> 'png' ,
		'raw'			=> base64_encode($qrUrl) ,
	);	
	$BD_zdh->logfile($orderId.'|'.json_encode($order),'bdzdh_orderqr_image_s');
	exit(json_encode($response_all));
	
}elseif($cmd == 'refund_apply'){	//退票申请

	$orderId = $msg['orderId'];						//百度订单号
	$auditId = $msg['auditId'];
	$refundNum = $msg['subOrders'][0]['refundNum'];
	$subOrderId = $msg['subOrders'][0]['subOrderId'];	//百度子订单	

	$pft_order_xml = $PFT_infunction->Select_REMOorder($orderId,22196);
	if(!$pft_order_xml) exit('{"error_code":"123"}');
	$pft_order = (string)$pft_order_xml->Rec->UUordernum ;
	$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
	$UUsellid = (int)$pft_order_xml->Rec->UUsellid;
	if($refundNum == $UUtnum)	$rnum = 0; //取消订单
	else	$rnum = $UUtnum - $refundNum ;
	
	$cancel = $PFT_infunction->Modify_Order($pft_order,$rnum);
	$BD_zdh->logfile($orderId.'|'.$pft_order.'|'.$cancel,'bdzdh_orderrefund_apply_s');
	if($cancel == 100){
		if($rnum == 0)$selorder = "update all_api_order set oStatus = 3  where pftOrder='$pft_order' limit 1";
		else $selorder = "update all_api_order set oStnum =$rnum where pftOrder='$pft_order' limit 1";
		$GLOBALS['le']->query($selorder);		
		$result = 1;
	}
	else $result = $cancel;
	
	echo '{"auditId":"'.$auditId.'","result":"'.$result.'"}';
	
	file_get_contents("http://www.12301.cc/module/api/bd_zhidahao/bdzdh_orderNote.php?cmd=refund&auditId=$auditId&status=$cancel&UUsellid=$UUsellid"); //退款通知
}
?>md=refund&auditId=$auditId&status=$cancel&UUsellid=$UUsellid"); //退款通知
}
?>